package com.xu.mylol.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xu.mylol.R;
import com.xu.mylol.bean.Video;

import java.util.ArrayList;

/**
 * Created by dllo on 15/12/8.
 */
public class MyListViewadapter extends BaseAdapter {
    private ArrayList<Video> datas;
    private Context context;

    public MyListViewadapter(ArrayList<Video> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

    //获取数量
    @Override
    public int getCount() {
        return datas.size() > 0 && datas != null ? datas.size() : 0;
    }

    //获取数据内容
    @Override
    public Object getItem(int position) {
        return datas.size() > 0 && datas != null ? datas.get(position) : null;
    }

    // 绑定ID
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;// 建立缓存类对象
        if (convertView == null) {//判断行布局是否为空
            //加载行布局文件
            convertView = LayoutInflater.from(context).inflate(R.layout.listview_item_video, null);
            //初始化缓存类对象
            holder = new ViewHolder();
            // 绑定行布局id
            holder.item_iv = (ImageView) convertView.findViewById(R.id.video_item_iv);
            holder.item_tv = (TextView) convertView.findViewById(R.id.video_item_tv);
            //将缓存类对象设置到行布局中
            convertView.setTag(holder);
        } else {
            //第二次运行 直接使用这些标签 不用再添加 直接使用
            holder = (ViewHolder) convertView.getTag();
        }

        //创建一个video 对象获取每一行的数据
        Video video = (Video) getItem(position);
        //设置数据
        if (video != null) {

            holder.item_iv.setImageResource(video.getImageid());
            holder.item_tv.setText(video.getTitle());


        }
        //得有返回语句
        return convertView;
    }

    //创建缓存类 先把数据保存一下  写行布局的属性参数
    private class ViewHolder {
        private TextView item_tv;
        private ImageView item_iv;
    }
}
